package monsters
{
	import org.flixel.*;
	import com.greensock.TweenMax;
	import com.greensock.easing.Sine;
	import com.greensock.easing.Linear;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Downer extends FlxSprite
	{
		
		[Embed(source = '../../assets/ET.png')] private var leftyPNG:Class;
		
		
		public var isDying:Boolean = false;
		
		private var yOriginal:int;
		private var xOriginal:int;
		private var isBacking:Boolean = false;
		private const inicialXVelocity:int = 30;
		private const inicialYVelocity:int = 50;
		private const rangeX:int = 3;
		private const rangeY:int = 20;
		
		
		public function Downer(x:int, y:int) 
		{
			super(x * 32, y * 32);
			
			yOriginal = y;
			xOriginal = x;
			
			loadGraphic(leftyPNG, true, true, 32, 23);
			
			addAnimation("walkNormal", [0, 1, 2, 3], 6, true);
			addAnimation("walkGelo", [4, 5, 6, 7], 6, true);
			addAnimation("walkFogo", [8, 9, 10, 11], 6, true);
			play("walkNormal");
			
			facing = FlxObject.RIGHT;
			
			isBacking = false;
			velocity.x = inicialXVelocity;
			velocity.y = inicialYVelocity;
			
			var random:Number = Math.random() * 20;
			
			if (random < 10) 
			{
				random = random * (-1); 
				
			}
			
			velocity.x += random;
			velocity.y += random;
			
		}
		
		public function change(controle:int):void
		{			
			if (controle <= 0) {
				init();		
				
			}else if (controle > 0 && controle <= 1) {
				initFire();
				
			}else if (controle > 1 && controle <= 2) {
				initGelo();
			}
			
			var random:Number = Math.random() * 20;
			
			if (random < 10) 
			{
				random = random * (-1); 
				
			}
			
			velocity.x += random;
			velocity.y += random;
		}
		
		private function init():void
		{	
			play("walkNormal");
			var tx:int = int(x / 32);
			var ty:int = int(y / 32);
			if (isBacking)
			{
				if (tx>xOriginal)
				{
					velocity.x = -10;
				}
				else
				{
					velocity.x = 10;
				}
				velocity.y = -90;
			}
			else
			{
				if (facing == FlxObject.RIGHT)
				{				
					velocity.x = inicialXVelocity;				
				}
				else
				{				
					velocity.x = -inicialXVelocity;
				}
				velocity.y = inicialYVelocity;
			}
			
		}
		
		private function initFire():void
		{			
			play("walkGelo");
			var tx:int = int(x / 32);
			var ty:int = int(y / 32);
			if (isBacking)
			{
				if (tx>xOriginal)
				{
					velocity.x = -10;
				}
				else
				{
					velocity.x = 10;
				}
				velocity.y = -90;
			}
			else
			{
				if (facing == FlxObject.RIGHT)
				{				
					velocity.x = inicialXVelocity;				
				}
				else
				{				
					velocity.x = -inicialXVelocity;
				}
				velocity.y = inicialYVelocity;
			}

		}
		
		private function initGelo():void
		{
			play("walkFogo");
			var tx:int = int(x / 32);
			var ty:int = int(y / 32);
			if (isBacking)
			{
				if (tx>xOriginal)
				{
					velocity.x = -10;
				}
				else
				{
					velocity.x = 10;
				}
				velocity.y = -90;
			}
			else
			{
				if (facing == FlxObject.RIGHT)
				{				
					velocity.x = inicialXVelocity;				
				}
				else
				{				
					velocity.x = -inicialXVelocity;
				}
				velocity.y = inicialYVelocity;
			}
		}
		
		
		override public function kill():void
		{
			FlxG.play(catty2SFX, 0.5, false, true);
			
			isDying = true;
			
			frame = 1;
			
			velocity.x = 0;
			velocity.y = 0;
			
			angle = 180;
			
			TweenMax.to(this, 1.5, { bezier: [ { x:"64", y:"-64" }, { x:"80", y:"200" } ], onComplete: removeSprite } );
			this.alive = false;
		}
		
		private function removeSprite():void
		{
			exists = false;
		}
		
		override public function update():void
		{
			super.update();
			
			//	Check the tiles on the left / right of it
			
			var tx:int = int(x / 32);
			var ty:int = int(y / 32);
			
			
			if (isBacking)
			{
				if (tx == xOriginal && ty == yOriginal)
				{
					isBacking = false;
					
					velocity.y = inicialYVelocity;
					
					if (Math.random() > 0.5)
					{
						velocity.x = inicialXVelocity;
						facing = FlxObject.RIGHT;
					}
					else
					{
						velocity.x = -inicialXVelocity;
						facing = FlxObject.LEFT;
					}
					
					
					
				}
				else if (tx == xOriginal)
				{
					velocity.x = 0;
				}
				else if(ty == yOriginal)
				{
					velocity.y = 0;
				}
				return;
			}
			else
			{
				if (ty > yOriginal + rangeY)
				{
					isBacking = true;
					if (tx>xOriginal)
					{
						velocity.x = -10;
					}
					else
					{
						velocity.x = 10;
					}
					velocity.y = -90;
				}
				else if (facing == FlxObject.RIGHT && tx > xOriginal + rangeX)
				{
					turnAround();					
				}
				else if (facing == FlxObject.LEFT && tx < xOriginal - rangeX)
				{
					turnAround();
				}
				trace("tx:"+tx);
				trace("range:"+(xOriginal - rangeX));
				return;
			}
			
			
		}
		
		private function turnAround():void
		{
			if (facing == FlxObject.RIGHT)
			{
				facing = FlxObject.LEFT;
				
				velocity.x = -1 * velocity.x;
			}
			else
			{
				facing = FlxObject.RIGHT;
				
				velocity.x = -1 * velocity.x;
			}
		}
		
	}

}